#pragma once
#include <vector>
#include <memory>

namespace safety {

class IOState
{
public:
    virtual bool check_safety_io() = 0;
};

class SafetyAction
{
public:
    enum class Action
    {
        PAUSE,
        STOP,
    };
    SafetyAction() = default;
    virtual ~SafetyAction() = default;
    int update();

    // 上升沿事件
    virtual int rising_event() = 0;
    // 下降沿事件
    virtual int falling_event() = 0;
    // 上升或下降沿触发后的计时处理事件
    virtual int watch_count_down() = 0;
    virtual int check_not_safe(bool& state) = 0;

protected:
    bool last_state_ = false;
    uint32_t count_down_ = 0;
    Action pause_or_stop_ = Action::STOP;
    // 与其关联的所有安全IO信号，均为安全才安全
    std::vector<std::shared_ptr<IOState>> io_state_;
};

class SafetyEventWatch
{
public:
    static SafetyEventWatch& instance();
    void init(int frq = 125);
    void watch();
    static int WatchFrq() { return safety_io_watch_frq_; }

private:
    // 监控频率
    static int safety_io_watch_frq_;
    std::vector<std::shared_ptr<SafetyAction>> events_;
};

}  // namespace safety